﻿using Excel2DbTool.Common;
using Excel2DbTool.Helpers;
using MvvmFoundation.Wpf;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace Excel2DbTool
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static readonly Messenger Messenger;

        static App()
        {
            Messenger = new Messenger();
        }

        public App()
        {
            DealWithTempDbTableLifecycle();

        }

        //会用到一个临时表用来保存Excel中的数据,然后再将临时表中保存的数据进行处理
        private void DealWithTempDbTableLifecycle()
        {
            this.Startup += App_Startup;
            this.Exit += App_Exit;
        }

        private void App_Exit(object sender, ExitEventArgs e)
        {
            MakeSureTableNotExist();
        }

        void App_Startup(object sender, StartupEventArgs e)
        {
            MakeSureTableNotExist();
            MakeSureTableExist();
        }

        void MakeSureTableNotExist()
        {
            try
            {
                SqlHelper.GetInstance().DumpTable();
            }
            catch (Exception ex)
            {
                DebugHelper.Write(ex.Message);
            }
        }

        void MakeSureTableExist()
        {
            try
            {
                SqlHelper.GetInstance().CreateTable();
            }
            catch (Excel2DbException ex)
            {
                DebugHelper.Write(ex.Message);
                MessageBox.Show(ex.Message);
            }
        }
    }



    public class CatchUnHandExceptionApp : Application
    {
        public CatchUnHandExceptionApp()
        {
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }


        #region 全局异常捕获
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                if (e.ExceptionObject is System.Exception)
                {
                    HandleException((System.Exception)e.ExceptionObject);
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                HandleException(e.Exception);
                e.Handled = true;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        public static void HandleException(Exception ex)
        {
            //记录日志
            if (!System.IO.Directory.Exists("Log"))
            {
                System.IO.Directory.CreateDirectory("Log");
            }
            DateTime now = DateTime.Now;
            string logpath = string.Format(@"Log\fatal_{0}{1}{2}.log", now.Year, now.Month, now.Day);
            System.IO.File.AppendAllText(logpath, string.Format("\r\n----------------------{0}--------------------------\r\n", now.ToString("yyyy-MM-dd HH:mm:ss")));
            System.IO.File.AppendAllText(logpath, ex.Message);
            if (ex.InnerException != null)
            {
                System.IO.File.AppendAllText(logpath, "\r\n");
                System.IO.File.AppendAllText(logpath, ex.InnerException.Message);
            }
            System.IO.File.AppendAllText(logpath, "\r\n");
            System.IO.File.AppendAllText(logpath, ex.StackTrace);
            System.IO.File.AppendAllText(logpath, "\r\n");
            System.IO.File.AppendAllText(logpath, "\r\n----------------------footer--------------------------\r\n");
        }
        #endregion
    }
}



